package com.heima.shop.service.impl;

import com.heima.shop.mapper.StoreMapper;
import com.heima.shop.service.StoreService;
import com.heima.common.exception.CustomException;
import com.heima.model.shop.dtos.StoreDto;
import com.heima.model.shop.pojos.Store;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements StoreService {

    /**
     * 查询列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findPage(StoreDto dto) {
        //1 参数检查
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 分页检查
        dto.checkParam();

        //2 执行查询
        // 分页
        Page page = new Page(dto.getPage(), dto.getSize());
        // 条件查询
        LambdaQueryWrapper<Store> wraaper = new LambdaQueryWrapper<>();

        // 门店全称
         if (StringUtils.isNotBlank(dto.getName())) {
            wraaper.like(Store::getName, dto.getName());
         }
        // 门店简称
         if (StringUtils.isNotBlank(dto.getSimpleName())) {
            wraaper.like(Store::getSimpleName, dto.getSimpleName());
         }
        // 门店编码
         if (StringUtils.isNotBlank(dto.getCode())) {
            wraaper.like(Store::getCode, dto.getCode());
         }
        // 门店图片
         if (StringUtils.isNotBlank(dto.getImgUrl())) {
            wraaper.like(Store::getImgUrl, dto.getImgUrl());
         }
        // 省
         if (StringUtils.isNotBlank(dto.getProvince())) {
            wraaper.like(Store::getProvince, dto.getProvince());
         }
        // 市
         if (StringUtils.isNotBlank(dto.getCity())) {
            wraaper.like(Store::getCity, dto.getCity());
         }
        // 区县
         if (StringUtils.isNotBlank(dto.getCounty())) {
            wraaper.like(Store::getCounty, dto.getCounty());
         }
        // 地址
         if (StringUtils.isNotBlank(dto.getAddress())) {
            wraaper.like(Store::getAddress, dto.getAddress());
         }
        // 城市编码
         if (StringUtils.isNotBlank(dto.getCityCode())) {
            wraaper.like(Store::getCityCode, dto.getCityCode());
         }
        // 联系人
         if (StringUtils.isNotBlank(dto.getContactName())) {
            wraaper.like(Store::getContactName, dto.getContactName());
         }
        // 联系电话
         if (StringUtils.isNotBlank(dto.getContactPhone())) {
            wraaper.like(Store::getContactPhone, dto.getContactPhone());
         }
        // 类别 1汽修厂 2配件商
         if (StringUtils.isNotBlank(dto.getType())) {
            wraaper.like(Store::getType, dto.getType());
         }
        // 是否连锁
         if (StringUtils.isNotBlank(dto.getIsChain())) {
            wraaper.like(Store::getIsChain, dto.getIsChain());
         }
        // 来源
         if (StringUtils.isNotBlank(dto.getSource())) {
            wraaper.like(Store::getSource, dto.getSource());
         }
        // 状态
         if (StringUtils.isNotBlank(dto.getStatus())) {
            wraaper.like(Store::getStatus, dto.getStatus());
         }

        // 类型标签
         if (dto.getTypeLab() != null) {
            wraaper.eq(Store::getTypeLab, dto.getTypeLab());
         }



        IPage pageResult = page(page, wraaper);

        //3 封装分页结果
        return PageResponseResult.page(dto.getPage(), dto.getSize(),
                (int) pageResult.getTotal(),pageResult.getRecords());
    }

    /**
     * 新增
     * @param store
     * @return
     */
    @Override
    public ResponseResult insert(Store store) {
        //1 参数检查
        if (store == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2 执行保存
        save(store);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(Store store) {

        //1.检查参数
        if(null == store){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.修改
        updateById(store);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Long id) {
        //1.检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
